﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using System.Data;
using NRCP.BusinessEntities;
using System.Globalization;

namespace NRCP
{
    public partial class Sampling_Plan_With_Location : System.Web.UI.Page
    {
        private Int32 _PLAN_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["_PLAN_ID"]);
            }
            set
            {
                ViewState["_PLAN_ID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            NationalPlanBLL bll = new NationalPlanBLL();

            ddlNationalPlanYear.DataSource = bll.GetAllNationalPlans();
            ddlNationalPlanYear.DataTextField = "PlanYear";
            ddlNationalPlanYear.DataValueField = "Id";
            ddlNationalPlanYear.DataBind();

            FishBLL fishBll = new FishBLL();
            ddlSampleType.DataSource = fishBll.GetAllFish();
            ddlSampleType.DataTextField = "Name";
            ddlSampleType.DataValueField = "Id";
            ddlSampleType.DataBind();

            DivisionBLL divisionBll = new DivisionBLL();
            ddlZones.DataSource = divisionBll.GetAllDivisions();
            ddlZones.DataTextField = "Name";
            ddlZones.DataValueField = "Id";
            ddlZones.DataBind();
        }

        protected void lnbShow_Click(object sender, EventArgs e)
        {
             NationalPlanBLL bll = new NationalPlanBLL();
            NationalPlan plan = bll.GetNationalPlanById(Convert.ToInt32(ddlNationalPlanYear.SelectedItem.Value));
            if (plan != null)
            {
                _PLAN_ID = plan.Id;
                NationalPlanDetailsMonthlyBLL monthlyBll = new NationalPlanDetailsMonthlyBLL();
                List<NationalPlanDetailsMonthly> monthlies = monthlyBll.GetMonthlyPlans(_PLAN_ID, Convert.ToInt32(ddlZones.SelectedItem.Value), Convert.ToInt32(ddlSampleType.SelectedItem.Value));
                grvSampleData.DataSource = GetFormatedPlan(monthlies);
                grvSampleData.DataBind();
            }
        }

        private DataTable GetFormatedPlan(List<NationalPlanDetailsMonthly> summary)
        {
            List<TempMonthScheduler> schedule = summary.ConvertAll(x => new TempMonthScheduler { Count=x.Count, Division=x.SubDistrict.Name, DivisionId=x.SubDistrictId.ToString(), MonthId=x.Month.ToString(), Group=x.NationalPlanDetailsYearly.NationalPlanChemicalGroup.ChemicalGroup.Name});

            List<String> uniqueUpazila = (from lst in summary select lst.SubDistrict.Name).Distinct().ToList<string>();
            
            DataTable dt = new DataTable();
            DataColumn dtColumnPreDef = new DataColumn("Upazila");
            dt.Columns.Add(dtColumnPreDef);

            var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.TakeWhile(m => m != String.Empty).Select((m, i) => new { Month = i + 1, MonthName = m }).ToList();
            foreach (var month in months)
            {
                DataColumn dtColumn = new DataColumn(month.MonthName);
                dt.Columns.Add(dtColumn);
            }
            foreach (var upazila in uniqueUpazila)
            {
                DataRow dr = dt.NewRow();
                dr[0] = upazila;

                List<TempMonthScheduler> upazilaSchedules =  schedule.Where(u => u.Division.Equals(upazila)).ToList();
                foreach (TempMonthScheduler upazilaSchedule in upazilaSchedules)
                {
                    dr[Convert.ToInt32(upazilaSchedule.MonthId)] = dr[Convert.ToInt32(upazilaSchedule.MonthId)].ToString()+", " + upazilaSchedule.Group;
                }

                dt.Rows.Add(dr);
            }
            return dt;
        }

    }
}